﻿using System;

namespace Carbon.Web.Forms.jQueryForms
{
    public class NumberEditor : TextBoxEditor
    {
		#region Properties (1)
		public override object Value
        {
            get
            {
                Type basicType = Editor.BasicType;

                String value = (String)base.Value;

                if (!string.IsNullOrEmpty(value))
                {
                    if (basicType == typeof(int))
                        return Convert.ToInt32((String)base.Value);
                    else if (basicType == typeof(long))
                        return Convert.ToInt64((String)base.Value);
                    else if (basicType == typeof(float))
                        return Convert.ToSingle((String)base.Value);
                    else if (basicType == typeof(double))
                        return Convert.ToDouble((String)base.Value);
                    else if (basicType == typeof(decimal))
                        return Convert.ToDecimal((String)base.Value);
                    else
                        throw new ArgumentOutOfRangeException("value");
                }
                else
                {
                    if (Editor.IsTypeNullable)
                    {
                        return null;
                    }
                    else
                    {
                        throw new NullReferenceException("Value cannot be empty.");
                    }
                }
            }
            set
            {
                if (this.Editor.ErrorDuringUpdateModel && !Editor.StoreInvalidValueInViewModel)
                {
                    value = this.Editor.Text;
                }

                Type basicType = Editor.BasicType;

                if (basicType == typeof(int) || basicType == typeof(long))
                    base.Value = String.Format(Editor.IntFormat, value);
                else if (basicType == typeof(float) || basicType == typeof(double) || basicType == typeof(decimal))
                    base.Value = String.Format(Editor.FloatFormat, value);
                else
                    throw new ArgumentOutOfRangeException("value");

                if (Editor.StoreInvalidValueInViewModel)
                {
                    if (Validate((String)base.Value))
                    {
                        this.Editor.ErrorDuringUpdateModel = false;
                        this.Editor.ErrorTextDuringUpdateModel = null;
                    }
                }
            }
        }

        private bool Validate(String value)
        {
            if (!this.Editor.IsTypeNullable && string.IsNullOrEmpty(value))
            {
                this.Editor.ErrorDuringUpdateModel = true;
                this.Editor.ErrorTextDuringUpdateModel = Errors.InvalidValue;
                return false;
            }

            return true;
        }

        protected override void ReturnValue()
        {
            if (!Validate(Editor.Text))
            {
                if (!Editor.StoreInvalidValueInViewModel)
                    return;
            }

            this.m_Value = Editor.Text;
        }
		#endregion

		#region Constructors (1)
		public NumberEditor(SimpleEditor editor)
            : base(editor)
        {

        }
		#endregion

		#region Methods (1)

		protected override string GetClientScript()
        {
            return string.Format("Carbon.Web.Forms.jQueryForms.NumberEditor(\"{0}\", {1});", this.ClientID, ConvertInvariant.ToString(this.Editor.CanBeNull).ToLower());
        }

		#endregion
    }
}